package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.common.LogInfo;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.enums.QrcodeEnum;
import com.beamfield.model.ManagerInfo;
import com.beamfield.model.NormalOperator;
import com.beamfield.model.SpecialOperator;
import com.beamfield.modelVo.Qrcode;
import com.beamfield.modelVo.QrcodeVo;
import com.beamfield.service.manage.device.DeviceService;
import com.beamfield.service.manage.operator.ManagerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author Zephyr
 * @Description: 移动 - - 二维码
 * @Date 2018/10/15
 */
@Controller
@RequestMapping(value = "/mb_api/qrcode")
public class MbQrCodeController extends MbBaseController {

    private final DeviceService deviceService;

    private final ManagerInfoService managerInfoService;

    @Autowired
    public MbQrCodeController(DeviceService deviceService, ManagerInfoService managerInfoService) {
        this.deviceService = deviceService;
        this.managerInfoService = managerInfoService;
    }

    /**
     * 根据二维码信息获取设备信息
     * @return
     */
    @RequestMapping(value = "/getQrcodeData", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getQrcodeData(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String qrCodeJson = body.getString("qrCodeJson");
        Qrcode qrcode = super.getQrcode(qrCodeJson);
        if(qrcode == null){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "无法解析二维码信息");
        }
        LogInfo logInfo = super.getLogInfo(request);
        Long beamId = logInfo.getBeamId();
        if(!Objects.equals(qrcode.getBeam(), String.valueOf(beamId))){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "此二维码不属于当前登录梁场");
        }
        try{
            String type = qrcode.getType();
            QrcodeVo vo = new QrcodeVo();
            String data = qrcode.getData();
            switch (QrcodeEnum.getByType(type)){
                case DEVICE_INBOUND:
                    vo.setData(deviceService.getDeviceInfoByInboundId(Long.parseLong(data)));
                    vo.setType(type);
                    return new ApiResult(vo);
                case USER_MANAGER:
                    ManagerInfo mbManagerInfo = managerInfoService.getMbManagerInfo(Long.parseLong(data), beamId);
                    if(mbManagerInfo != null){
                        vo.setData(mbManagerInfo.getEcode());
                    }
                    vo.setType(type);
                    return new ApiResult(vo);
                case USER_SPECIAL:
                    SpecialOperator mbSpecialOperatorInfo = managerInfoService.getMbSpecialOperatorInfo(Long.parseLong(data), beamId);
                    if(mbSpecialOperatorInfo != null){
                        vo.setData(mbSpecialOperatorInfo.getEcode());
                    }
                    vo.setType(type);
                    return new ApiResult(vo);
                case USER_NORMAL:
                    NormalOperator mbNormalOperatorInfo = managerInfoService.getMbNormalOperatorInfo(Long.parseLong(data), beamId);
                    if(mbNormalOperatorInfo != null){
                        vo.setData(mbNormalOperatorInfo.getEcode());
                    }
                    vo.setType(type);
                    return new ApiResult(vo);
                case DOCUMENT:
                case PROCNOTICE:
                    vo.setType(type);
                    vo.setData(data);
                    return new ApiResult(vo);
                case MATERIAL_EMBED:

                case NULL: return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "无法解析二维码信息");
                default: return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "无法解析二维码信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
}
